package main.com.oaer.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页封装类
 * @author Administrator
 *
 */
public class PageList {
     
	/**
	 * 当前页
	 */
	private int currentPage = 1;
	
	/**
	 * 每页显示的数量
	 */
	private int pageSize;
	
	/**
	 * 总记录数
	 */
	private Long totalSize = 0L;
	
	/**
	 * 总页数
	 */
	private int totalPage;
	
	/**
	 * 有没有首页
	 */
	private boolean hasFirst;
	
	/**
	 * 有没有末页
	 */
	private boolean hasLast;
	
	/**
	 * 有没有下一页
	 */
	private boolean hasPrevious;
	
	/**
	 * 有没有上一页
	 */
	private boolean hasNext;
	
	/**
	 * 分页显示的每一页
	 */
	private List<Integer> numbers = new ArrayList<Integer>();
	
	/**
	 * 储存分页显示的对象
	 */
	private List<Object> list = new ArrayList<Object>();
	
	/**
	 * 扩展存放其他对象
	 */
	private Map<Object,Object> map = new LinkedHashMap<Object,Object>();
	
	public PageList(int currentPage, Long totalSize, int pageSize){
		if(currentPage <= 0){
			currentPage = 1;
		}
		if(pageSize <=0 ){
			pageSize = 1;
		}
		this.currentPage = currentPage;
		this.totalSize = totalSize;
		this.pageSize = pageSize;		
	}
	
	//需不需要显示首页
	public boolean isHasFirst(){
		if( currentPage == 1 || this.getTotalPage() == 0){
			return false;
		}
		return true;
	}
	
	//需不需要显示尾页
	public boolean isHasLast(){
		if( this.getTotalPage() == 0 || currentPage == totalPage || totalPage == 0){
			return false;
		}
		return true;		
	}
	
	//是否显示上一页
	public boolean isHasPrevious(){
		if(this.isHasFirst()){
			return true;
		}
		return false;
	}
	
	//是否显示下一页
	public boolean isHasNext(){
		if(this.isHasLast()){
			return true;
		}
		return false;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Long getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(Long totalSize) {
		this.totalSize = totalSize;
	}
   /**
    * 总页数
    * @return
    */
	public int getTotalPage() {
		totalPage = (int) (totalSize/pageSize);
		if(totalSize % pageSize !=0){
			totalPage++;
		}
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public List<Integer> getNumbers() {
		//当超过10页并且当前页大于2的话
		if(this.getTotalPage() > 10 && this.currentPage > 2){
			for(int i=this.currentPage - 2;i<this.currentPage+8 && i<=this.getTotalPage(); i++){
				numbers.add(i);
			}
		}else{
			for(int i=1; i<=this.getTotalPage() && i<= 10; i++){
				numbers.add(i);
			}
		}
		return numbers;
	}

	public void setNumbers(List<Integer> numbers) {
		this.numbers = numbers;
	}

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}

	public Map<Object, Object> getMap() {
		return map;
	}

	public void setMap(Map<Object, Object> map) {
		this.map = map;
	}

	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}

	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}
}
